# -*- coding: utf-8 -*-
# File : job_util.py
# Author: taoyahui
# Date : 2021/7/14
from utils import mysql_util, common_util
import results
import json
import datetime


def strategy_job_find(s_id):
    """
    策略任务搜索
    :param s_id:
    :return:
    """
    find_sql = f"select * from task_info where strategy_id = {s_id}"
    res_list = mysql_util.exec_sql_new(find_sql)
    if len(res_list) == 0:
        return results.job_not_exist_error
    return res_list[0]


def add_scan_job(task_name, hostname, ip, cron_rel):
    find_scan_job_sql = f"select * from task_info where hostname = '{hostname}'"
    res = mysql_util.exec_sql_new(find_scan_job_sql)
    if len(res) > 0:
        # 任务已存在，修改
        task_name = f'安检任务-{task_name}'
        update_sql = f"update task_info set execute_time = '{json.dumps(cron_rel)}' where task_name = '{task_name}'"
        mysql_util.exec_sql_new(update_sql)
        return results.success_result
    # 任务不存在，加入
    scan_job_dict = {
        'task_name': f'安检任务-{task_name}',
        'execute_time': json.dumps(cron_rel),
        'task_type': '1',
        'execute_fun': 0,
        'hostname': hostname,
        'ip': ip
    }
    insert_sql = mysql_util.generate_insert_sql(scan_job_dict, 'task_info')
    mysql_util.exec_sql_params(insert_sql,tuple(scan_job_dict.values()))
    return results.success_result


def strategy_job_exists(s_id):
    find_scan_job_sql = f"select * from task_info where strategy_id = '{s_id}'"
    res = mysql_util.exec_sql_new(find_scan_job_sql)
    if len(res) == 0:
        return results.job_not_exist_error


def remove_scan_job(hostname):
    """
    删除漏扫任务
    :param task_name:
    :return:
    """
    find_scan_job_sql = f"select * from task_info where hostname = '{hostname}'"
    res = mysql_util.exec_sql_new(find_scan_job_sql)
    if len(res) == 0:
        return results.job_not_exist_error
    delete_sql = f"Delete from task_info where hostname = '{hostname}'"
    mysql_util.exec_sql_new(delete_sql)
    return results.success_result


def add_strategy_job_record(task_name, cron_rel, execute_fun, s_id):
    find_strategy_job_task = f"Select * from task_info where strategy_id = '{s_id}'"
    res_list = mysql_util.exec_sql_new(find_strategy_job_task)
    if 'run_date' in cron_rel:
        cron_rel['run_date'] = datetime.datetime.strftime(cron_rel['run_date'], '%Y-%m-%d %H:%M:%S')
    # 如果策略已存在，就更新
    if len(res_list) > 0:
        update_sql = f"update task_info set execute_time = '{json.dumps(cron_rel)}', execute_fun = {execute_fun} " \
                     f"where strategy_id = {s_id}"
        mysql_util.exec_sql_new(update_sql)
        return results.success_result
    strategy_dict = {
        'task_name': f'补丁任务-{task_name}',
        'execute_time': json.dumps(cron_rel),
        'task_type': '2',
        'execute_fun': execute_fun,
        'strategy_id': s_id
    }
    insert_sql = mysql_util.generate_insert_sql(strategy_dict, 'task_info')
    mysql_util.exec_sql_params(insert_sql, tuple(strategy_dict.values()))
    return results.success_result


def remove_strategy_execute_job(s_id):
    """
    删除策略执行任务
    :param s_id:
    :return:
    """
    find_strategy_job_task = f"Select * from task_info where strategy_id = '{s_id}'"
    res_list = mysql_util.exec_sql_new(find_strategy_job_task)
    if len(res_list) == 0:
        return results.job_not_exist_error
    delete_sql = f"Delete from task_info where strategy_id = {s_id}"
    mysql_util.exec_sql_new(delete_sql)
    return results.success_result





